package com.dididoctor.patient.Activity.Usercentre.Consume;

import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.MV.EduActivity;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Ui.customlistview.OnRefreshListener;
import com.dididoctor.patient.Ui.customlistview.RefreshListView;
import com.dididoctor.patient.Utils.MyToast;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： UserConsumeActivity
 * @包名称： com.dididoctor.patient.Activity.Usercentre.Consume
 * @创建人： zhanjunjie
 * @创建时间：2016/4/5 11:41
 * @修改人：
 * @修改时间：2016/4/5 11:41
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class UserConsumeActivity extends EduActivity implements UserConsumeView, UserConsumeAdapter.Paybacklistenct {

    private RefreshListView listView;
    private UserConsumeAdapter adapter;
    private UserConsumePresenter presenter;
    private int pageIndex = 1;
    private List<Consume> consumeList = new ArrayList<>();

    private LinearLayout ll_on_order;
    private Consume consume;
    private static final int TO_RESULT = 1005;

    @Override
    public void init() {

        initview();

    }

    private void initview() {
        setText(R.id.tv_title, "消费记录");
        ll_on_order = (LinearLayout) findViewById(R.id.ll_on_order);
        listView = (RefreshListView) findViewById(R.id.listview);
        listView.setHeadAndFoot(true, true);
        listView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                presenter.getConsume(pageIndex);
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                presenter.getConsume(pageIndex);
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        adapter = new UserConsumeAdapter(this, consumeList, this);
        listView.setAdapter(adapter);
        presenter = new UserConsumePresenter(this, this);
        presenter.getConsume(pageIndex);
    }




    @Override
    public void onClickEvent(View view) {

    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_user_consume);
    }

    @Override
    public void getConsume(List<Consume> consumes) {
        if (consumes != null && consumes.size() > 0) {
            //没有评价内容
            ll_on_order.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            if (pageIndex == 1) {
                this.consumeList.clear();
                adapter.setData(consumes);
                listView.onRefreshFinish();
            }else{
                if (consumes.size() < ConstantValue.LOAD_MESSAGE_COUNT) {
                    adapter.addData(consumes);
                    listView.showNoMoreData();
                } else {
                    adapter.addData(consumes);
                    listView.hideNoMoreData();
                }
            }
            adapter.notifyDataSetChanged();
        } else {
            if (pageIndex == 1) {
                ll_on_order.setVisibility(View.VISIBLE);
                listView.setVisibility(View.GONE);
            }else{
                listView.showNoMoreData();
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getConsumefail() {
        listView.onRefreshFinish();
        MyToast.DefaultmakeText(UserConsumeActivity.this, "您的网络异常请稍候再试！", 0);
    }

    @Override
    public void getConsumeno() {
        showToastMessage("加载完成");
        listView.onRefreshFinish();
    }

    @Override
    public void paybackz() {
        Intent intent = new Intent(this, WithdrawalsSuccedActivity.class);
        intent.putExtra("from", "1");
        intent.putExtra("balance", consume.getMoney());
        startActivityForResult(intent, TO_RESULT);
    }

    @Override
    public void paybackzfail() {
        Intent intent = new Intent(this, WithdrawalsFailActivity.class);
        intent.putExtra("from", "1");
        startActivityForResult(intent, TO_RESULT);
    }

    @Override
    public void paybackw() {
        Intent intent = new Intent(this, WithdrawalsSuccedActivity.class);
        intent.putExtra("from", "1");
        intent.putExtra("balance", consume.getMoney());
        startActivityForResult(intent, TO_RESULT);
    }

    @Override
    public void paybackwfail() {
        Intent intent = new Intent(this, WithdrawalsFailActivity.class);
        intent.putExtra("from", "1");
        startActivityForResult(intent, TO_RESULT);
    }

    @Override
    public void showNoData() {
        listView.showNoMoreData();
    }

    @Override
    public void payback(Consume prdId) {
        consume = prdId;
        if ("1".equals(prdId.getPayType())) {//支付宝
            presenter.alipayback(prdId.getPayId());
        } else {
            presenter.payback(prdId.getPayId(), prdId.getOrderId());
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        pageIndex =1;
        presenter.getConsume(pageIndex);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TO_RESULT && resultCode == RESULT_OK) {
            presenter.getConsume(pageIndex);
        }
    }
}
